#conding=utf-8
import xlrd
from xlutils.copy import copy


class ExcelUtil:
    def __init__(self,excel_path=None,index=None):
        if excel_path == None:
            self.excel_path="D:/seleniumpython/config/casedata.xls"
        else:
            self.excel_path = excel_path
        if index == None:
            index = 0
        self.data = xlrd.open_workbook(self.excel_path)
        self.table = self.data.sheets()[index]
        
    
    def get_data(self):
        result=[]
        rows = self.get_lines()
        if rows!=None:
            for i in range(rows):
                col = self.table.row_values(i)
                print(col)
                result.append(col)
            return result
        return None

    #获取行数
    def get_lines(self):
        #行数
        rows = self.table.nrows
        #判断文件里是否有数据
        if rows>=1:
            return rows
        return None

    #获取单元格信息
    def get_col_value(self,rows,col):
        #判断文件里边的行数大于输入的行数，若是小于则数据为空
        if self.get_lines()>rows:
            data = self.table.cell(rows,col).value
            return data
        return None

    #写入数据 pip install xlutils
    def write_value(self,row,value):
        read_value = xlrd.open_workbook(self.excel_path)
        write_data = copy(read_value)
        write_data.get_sheet(0).write(row,9,value)
        write_data.save(self.excel_path)


if __name__ == "__main__":
    ex = ExcelUtil('D:/seleniumpython/config/keyword.xls')
    print(ex.write_value(7,"test"))
